package com.msxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msxt.dto.YudingxinxiEntity;
import com.msxt.mapper.YudingxinxiDao;
import com.msxt.result.Query;
import com.msxt.service.YudingxinxiService;
import com.msxt.utils.PageUtils;
import com.msxt.view.YudingxinxiView;
import com.msxt.vo.YudingxinxiVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service("yudingxinxiService")
@Transactional
public class YudingxinxiServiceImpl extends ServiceImpl<YudingxinxiDao, YudingxinxiEntity>
		implements YudingxinxiService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		Page<YudingxinxiEntity> page = new Query<YudingxinxiEntity>(params).getPage();
		Page<YudingxinxiEntity> result = this.page(page, new QueryWrapper<>());
		return new PageUtils(result);
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params, QueryWrapper<YudingxinxiEntity> wrapper) {
		// 1. 构造分页参数
		Page<YudingxinxiView> page = new Query<YudingxinxiView>(params).getPage();

		// 2. 处理排序参数
		String sort = (String) params.get("sort");
		String order = (String) params.get("order");
		if (StringUtils.isNotBlank(sort)) {
			boolean isAsc = "asc".equalsIgnoreCase(order);
			wrapper.orderBy(true, isAsc, sort);
		}

		// 3. 执行分页查询
		IPage<YudingxinxiView> result = baseMapper.selectPageView(page, wrapper);
		return new PageUtils(result);
	}

	@Override
	public List<YudingxinxiVO> listVO(QueryWrapper<YudingxinxiEntity> wrapper) {
		return baseMapper.listVO(wrapper);
	}

	@Override
	public YudingxinxiVO selectVO(QueryWrapper<YudingxinxiEntity> wrapper) {
		return baseMapper.selectVO(wrapper);
	}

	@Override
	public List<YudingxinxiView> listView(QueryWrapper<YudingxinxiEntity> wrapper) {
		return baseMapper.listView(wrapper);
	}

	@Override
	public YudingxinxiView selectView(QueryWrapper<YudingxinxiEntity> wrapper) {
		return baseMapper.selectView(wrapper);
	}
}